using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WPCoco.Graphics
{
    /// <summary>
    /// Interface for using the AtlasSprite as client
    /// </summary>
    public interface IAtlasSpriteClient : ITextureReference, IContainer<IAtlasSpriteClient>, IReverseContainer<IAtlasSpriteClient>
    {
        /// <summary>
        /// Referenced AtlasSprite 
        /// </summary>
        AtlasSprite Atlas { get; set; }

        /// <summary>
        /// Index in the AtlasSprite Structure
        /// </summary>
        int AtlasIndex { get; set; }

        /// <summary>
        /// Parent Client, and client must allways have his parent linked to an AtlasSprite.
        /// Exception: The AtlasSprite owner 
        /// </summary>
        IAtlasSpriteClient ParentClient { get; set; }

        /// <summary>
        /// This is used for inserting an client next to his brother linked
        /// </summary>
        IAtlasSpriteClient GetNextNodeLinked(IAtlasSpriteClient client);

        /// <summary>
        /// AtlasQuad Struct used for drawing the content in the AtlasSprite
        /// </summary>
        AtlasQuad GetQuad();

        /// <summary>
        /// For reporting change in some components in the client, this must be reflected in
        /// his childrens
        /// </summary>
        /// <param name="component">Component that has changed</param>
        void ReportChange(QuadDirtyComponent component);

        /// <summary>
        /// Request made by your parent when it is using an AtlasSprite
        /// </summary>
        void RequestRegister(AtlasSprite atlas, IAtlasSpriteClient parent);
    }

    public static class IAtlasSpriteClientExtensions 
    {
        /// <summary>
        /// Is using an AtlasSprite
        /// </summary>
        public static bool IsAtlasUsed(this IAtlasSpriteClient client)
        {
            if (client == null) return false;
            return client.Atlas != null;
        }

        /// <summary>
        /// Is using an AtlasSprite and its registered now
        /// </summary>
        public static bool IsAtlasLinked(this IAtlasSpriteClient client)
        {
            if (client == null) return false;
            return client.AtlasIndex != -1;
        }

        /// <summary>
        /// Its owner of an AtlasSprite
        /// </summary>
        public static bool IsAtlasOwner(this IAtlasSpriteClient client) 
        {
            if (client == null) return false;
            return client.Atlas != null && client.Atlas.Owner == client;
        }

        /// <summary>
        /// Can be registered to and AtlasSprite, this is because is its oner o
        /// his parent is alleady registered
        /// </summary>
        public static bool CanRegister(this IAtlasSpriteClient client)
        {
            if (client.IsAtlasOwner() && !client.IsAtlasLinked()) return true;
            if (client.ParentClient != null && client.ParentClient.IsAtlasLinked() && !client.IsAtlasLinked()) return true;
            return false;
        }
    }
}
